/*
 * NewsViewer.java
 *
 * Created on 19-mar-2009, 11:19:43
 */

package grupb.upf.gui;

import grupb.upf.XMLObjects.Author;
import grupb.upf.XMLObjects.New;
import grupb.upf.XMLObjects.PhotoNew;
import grupb.upf.XMLObjects.Tag;
import grupb.upf.XMLObjects.Tagging;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * <p>This class contains the news viewer.</p>
 *
 * @version    0.0.1-SNAPSHOT
 * @author Group B
 */
public class NewsViewer extends javax.swing.JPanel {

    /**
     * The logger. A new logger <code>grupb.upf.gui.NewsViewer</code>
     * is created to add the logs of this NewsViewer
     */
    private static final Log LOG = LogFactory.getLog(NewsViewer.class);
    /**
     * The principal GUI.
     */
    private P2Piki p2piki;
    /**
     * The list of tags.
     */
    private List < Tag > auxTagsList = new Vector();
    /**
     * The list of tags.
     */
    private List < Author > auxAuthorsList = new Vector();
    /**
     * The default list model of edited tags.
     */
    private DefaultListModel editedTagsModel = new DefaultListModel();
    /**
     * The default list model of edited tags.
     */
    private DefaultListModel authorsModel = new DefaultListModel();
    /**
     * The new.
     */
    private New nw;

    /** Creates new form NewsViewer */
    public NewsViewer(final P2Piki p2p) {
        this.p2piki = p2p;
        initComponents();
    }
    /**
     * Method to view a new.
     * @param newNew the new to view.
     */
    public void setNew(final New newNew) {
        nw = newNew;
        getProperties();
        p2piki.repaint();
    }

    /**
     * Method to get the properties and show them.
     */
    private void getProperties() {
        if (nw != null) {
            title.setText(nw.getTitle());
            date.setText(getDate(nw.getDate()));
            subtitle.setText(nw.getSubtitle());
            source.setText(nw.getSource());
            List e = nw.getBody().getElements();
            body.setText("");
            for (int i = 0; i < e.size(); i++) {
                if (e.get(i) instanceof String) {
                    try {
                        body.getStyledDocument().insertString(
                                body.getStyledDocument().getLength(),
                                (String) e.get(i) + "\n\n",
                                new SimpleAttributeSet());
                    } catch (BadLocationException ex) {
                        if (LOG.isErrorEnabled()) {
                            LOG.error("Error inserting the text.", ex);
                        }
                    }
                } else if (e.get(i) instanceof PhotoNew) {
                    PhotoNew pn = (PhotoNew) e.get(i);
                    body.insertIcon(pn.getIcon());
                    try {
                        body.getStyledDocument().insertString(
                                body.getStyledDocument().getLength(), "\n\n",
                                new SimpleAttributeSet());
                    } catch (BadLocationException ex) {
                        if (LOG.isErrorEnabled()) {
                            LOG.error("Error inserting the space.", ex);
                        }
                    }
                }
            }
            Tagging tags = nw.getTags();
            List < Tag > listOfTags = tags.getPersonalTags();
            List < Author > listOfAuthors = nw.getAuthors();
            auxAuthorsList = new Vector();
            auxTagsList = new Vector();
            authorsModel.removeAllElements();
            editedTagsModel.removeAllElements();
            for (int i = 0; i < listOfTags.size(); i++) {
                auxTagsList.add(listOfTags.get(i));
                editedTagsModel.addElement(
                        (String) listOfTags.get(i).getTag());
            }
            listOfTags = tags.getPredefinedTags();
            for (int i = 0; i < listOfTags.size(); i++) {
                auxTagsList.add(listOfTags.get(i));
                editedTagsModel.addElement(
                        (String) listOfTags.get(i).getTag());
            }
            for (int i = 0; i < listOfAuthors.size(); i++) {
                auxAuthorsList.add(listOfAuthors.get(i));
                authorsModel.addElement((String) listOfAuthors.get(i).getName()
                        + " " + listOfAuthors.get(i).getSurname() + ": "
                        + listOfAuthors.get(i).getAccount());
            }
        }
    }

    /**
     * Method to get the date from an existent String date.
     * @param date the date in String format (YYYYMMDD)
     * @return the date formatted to (DD-MM-YYYY)
     */
    private String getDate(final String date) {
        String newDate = "(" + date.substring(6, 8) + "/" +
                date.substring(4, 6) + "/" + date.substring(0, 4) + ")";
        return newDate;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        newsViewPanel = new javax.swing.JPanel();
        optionsPanel = new javax.swing.JPanel();
        newsListButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        source = new javax.swing.JLabel();
        editedTagsScrollPane1 = new javax.swing.JScrollPane();
        editedTagsList = new javax.swing.JList();
        editedTagsScrollPane2 = new javax.swing.JScrollPane();
        authorsList = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        body = new javax.swing.JTextPane();
        jPanel2 = new javax.swing.JPanel();
        title = new javax.swing.JLabel();
        date = new javax.swing.JLabel();
        subtitle = new javax.swing.JLabel();

        newsViewPanel.setBackground(new java.awt.Color(255, 255, 255));
        newsViewPanel.setMaximumSize(new java.awt.Dimension(680, 500));
        newsViewPanel.setMinimumSize(new java.awt.Dimension(680, 500));
        newsViewPanel.setPreferredSize(new java.awt.Dimension(680, 500));

        optionsPanel.setBackground(new java.awt.Color(255, 255, 255));
        optionsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Options", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        newsListButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/listNews.png"))); // NOI18N
        newsListButton.setText("List of news");
        newsListButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newsListButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout optionsPanelLayout = new javax.swing.GroupLayout(optionsPanel);
        optionsPanel.setLayout(optionsPanelLayout);
        optionsPanelLayout.setHorizontalGroup(
            optionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(optionsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(newsListButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        optionsPanelLayout.setVerticalGroup(
            optionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(optionsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(newsListButton, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Source: ");

        source.setText("jLabel2");

        editedTagsScrollPane1.setMaximumSize(new java.awt.Dimension(144, 130));
        editedTagsScrollPane1.setMinimumSize(new java.awt.Dimension(144, 130));
        editedTagsScrollPane1.setPreferredSize(new java.awt.Dimension(144, 130));

        editedTagsList.setModel(editedTagsModel);
        editedTagsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        editedTagsList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                editedTagsListValueChanged(evt);
            }
        });
        editedTagsList.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                editedTagsListFocusLost(evt);
            }
        });
        editedTagsScrollPane1.setViewportView(editedTagsList);

        editedTagsScrollPane2.setMaximumSize(new java.awt.Dimension(144, 130));
        editedTagsScrollPane2.setMinimumSize(new java.awt.Dimension(144, 130));
        editedTagsScrollPane2.setPreferredSize(new java.awt.Dimension(144, 130));

        authorsList.setModel(authorsModel);
        authorsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        authorsList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                authorsListValueChanged(evt);
            }
        });
        authorsList.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                authorsListFocusLost(evt);
            }
        });
        editedTagsScrollPane2.setViewportView(authorsList);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Authors");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("Tags");

        body.setEditable(false);
        body.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jScrollPane1.setViewportView(body);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(source)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 133, Short.MAX_VALUE)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(editedTagsScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(editedTagsScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 670, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel1)
                                    .addComponent(source))
                                .addGap(122, 122, 122))
                            .addComponent(editedTagsScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(133, 133, 133))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(editedTagsScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
                        .addContainerGap())))
        );

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        title.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        title.setText("Title");

        date.setText("(02 - 06 - 1986)");

        subtitle.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        subtitle.setText("jLabel1");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(subtitle, javax.swing.GroupLayout.PREFERRED_SIZE, 459, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(title)
                        .addGap(26, 26, 26)
                        .addComponent(date)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(title)
                    .addComponent(date))
                .addGap(18, 18, 18)
                .addComponent(subtitle, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout newsViewPanelLayout = new javax.swing.GroupLayout(newsViewPanel);
        newsViewPanel.setLayout(newsViewPanelLayout);
        newsViewPanelLayout.setHorizontalGroup(
            newsViewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, newsViewPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(newsViewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(newsViewPanelLayout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(optionsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        newsViewPanelLayout.setVerticalGroup(
            newsViewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(newsViewPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(newsViewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(optionsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(newsViewPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(newsViewPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void newsListButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newsListButtonActionPerformed
        // TODO add your handling code here:
        p2piki.viewListNews();
}//GEN-LAST:event_newsListButtonActionPerformed

    private void editedTagsListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_editedTagsListValueChanged
        // TODO add your handling code here:
}//GEN-LAST:event_editedTagsListValueChanged

    private void editedTagsListFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_editedTagsListFocusLost
        // TODO add your handling code here:
}//GEN-LAST:event_editedTagsListFocusLost

    private void authorsListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_authorsListValueChanged
        // TODO add your handling code here:
}//GEN-LAST:event_authorsListValueChanged

    private void authorsListFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_authorsListFocusLost
        // TODO add your handling code here:
}//GEN-LAST:event_authorsListFocusLost


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList authorsList;
    private javax.swing.JTextPane body;
    private javax.swing.JLabel date;
    private javax.swing.JList editedTagsList;
    private javax.swing.JScrollPane editedTagsScrollPane1;
    private javax.swing.JScrollPane editedTagsScrollPane2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton newsListButton;
    private javax.swing.JPanel newsViewPanel;
    private javax.swing.JPanel optionsPanel;
    private javax.swing.JLabel source;
    private javax.swing.JLabel subtitle;
    private javax.swing.JLabel title;
    // End of variables declaration//GEN-END:variables

}
